package com.lbd.finance.service.impl;

import com.lbd.base.service.BaseServiceImpl;
import com.lbd.finance.bean.UserTradeDetail;
import com.lbd.finance.bean.vo.UserTradeDetailVo;
import com.lbd.finance.dao.UserTradeDetailRepository;
import com.lbd.finance.service.UserTradeDetailService;
import com.lbd.system.bean.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 用户交易记录
 */
@Service
@Transactional
public class UserTradeDetailServiceImpl extends BaseServiceImpl<UserTradeDetail, UserTradeDetailRepository> implements UserTradeDetailService {
    @Autowired
    UserTradeDetailRepository usertradedetailRepository;

    @Override
    public Specification<UserTradeDetail> getSpecification(UserTradeDetail usertradedetail) {
        return null;
    }

    /**
     * 构建查询语句和参数
     *
     * @param usertradedetail
     * @param parameter
     * @return
     */
    @Override
    public String buildJpql(UserTradeDetail usertradedetail, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder("  from  UserTradeDetail t ");
        if (usertradedetail != null) {
            UserTradeDetailVo utdVo = (UserTradeDetailVo) usertradedetail;
            User u = utdVo.getUser();
            if (u != null) {
                if (StringUtils.isNotBlank(u.getTelephone())) {
                    jpqlBuilder.append(" and t.user.telephone like :telephone ");
                    parameter.put("telephone", "%".concat(u.getTelephone()).concat("%"));
                }
            }
            if (utdVo.getTrdType() != null) {
                jpqlBuilder.append(" and t.trdType = :trdType ");
                parameter.put("trdType", utdVo.getTrdType());
            }
            if (utdVo.getBeginTime() != null) {
                jpqlBuilder.append(" and t.trdTime >= :beginTime ");
                //日期格式为yyyy-MM-dd 需要转换一下
                parameter.put("beginTime", utdVo.getBeginTime());
            }
            if (utdVo.getEndTime() != null) {
                jpqlBuilder.append(" and t.trdTime <= :endTime ");
                //日期格式为yyyy-MM-dd 需要转换一下
                parameter.put("endTime", utdVo.getEndTime());
            }
        }
        jpqlBuilder.append(" order by trdTime desc ");
        return jpqlBuilder.toString();
    }

}
